package org.jasig.cas.cuscon;

import org.apache.commons.lang3.BooleanUtils;
import org.apache.commons.lang3.StringUtils;
import org.jasig.cas.authentication.principal.Service;
import org.jasig.cas.web.support.WebUtils;
import org.springframework.webflow.action.AbstractAction;
import org.springframework.webflow.execution.Event;

import javax.servlet.http.HttpServletRequest;

/**
 * Created by Administrator on 2017/5/25/025.
 */
public class AjaxLoginServiceTicketAction extends AbstractAction {

    // The default call back function name.
    protected static final String J_CALLBACK = "feedBackUrlCallBack";

    @Override
    protected Event doExecute(org.springframework.webflow.execution.RequestContext requestContext) throws Exception {
        HttpServletRequest request = WebUtils.getHttpServletRequest(requestContext);
        Event event = requestContext.getCurrentEvent();
        boolean isAjax = BooleanUtils.toBoolean(request.getParameter("isajax"));

        if (!isAjax){  // 非 ajax/iframe 方式登录，返回当前 event.
            return event;
        }
        boolean isLoginSuccess;
        // Login Successful.
        if ("success".equals(event.getId())){ //是否登录成功
            final Service service = WebUtils.getService(requestContext);
            final String serviceTicket = WebUtils.getServiceTicketFromRequestScope(requestContext);
            if (service != null){  //设置登录成功之后 跳转的地址
                request.setAttribute("service", service.getId());
            }
            request.setAttribute("ticket", serviceTicket);
            isLoginSuccess = true;
        } else { // Login Fails..
            isLoginSuccess = false;
        }

        boolean isFrame = BooleanUtils.toBoolean(request.getParameter("isframe"));
        String callback = request.getParameter("callback");
        if(StringUtils.isEmpty(callback)){ // 如果未转入 callback 参数，则采用默认 callback 函数名
            callback = J_CALLBACK;
        }
        if(isFrame){ // 如果采用了 iframe ，则 concat 其 parent 。
            callback = "parent.".concat(callback);
        }
        request.setAttribute("isFrame", isFrame);
        request.setAttribute("callback", callback);
        request.setAttribute("isLogin", isLoginSuccess);

        return new Event(this, "local"); // 转入 ajaxLogin.jsp 页面
    }
}
